﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WRJ
{
	public partial class ManageUser : BaseDBUserControl
	{
		private MainMenuDB ManageMenu;
		private Button ScoreButton;
		private Button ViewPasswordButton,ShowPasswordButton;
		string[] title={"系统编号","用户名","首次登录","上次登录","考核起始时间","临时登录","备注"};
		
		public ManageUser(MainMenuDB menu,Button button)
		{
			InitializeComponent();
			
			ManageMenu=menu;
			ScoreButton=button;
			menu.Text = "管理员模式 —— 用户管理";

			dataGridView1.DataSource = databaseWRJDataSet1.UserInfo;
						
			for (int i = 0; i < title.Length; i++)
			{
				dataGridView1.Columns[i].HeaderText = title[i];
				dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =DataGridViewContentAlignment.MiddleCenter;
			}
			
			userInfoTableAdapter1.Fill(databaseWRJDataSet1.UserInfo);

			AcceptButton.Click += new EventHandler(AcceptButton_Click);
			dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged);
			
			ViewPasswordButton = new Button();
			ViewPasswordButton.Text = "查看选中用户登录密码";
			ViewPasswordButton.Size = new Size(ViewPasswordButton.Size.Width,ViewPasswordButton.Size.Height*2);
			ViewPasswordButton.Click += new EventHandler(ViewPasswordButton_Click);

			ShowPasswordButton = new Button();
			ShowPasswordButton.Text = "密码对照表";
			ShowPasswordButton.Click += new EventHandler(ShowPasswordButton_Click);
			
			flowLayoutPanel1.Controls.Add(ViewPasswordButton);
			flowLayoutPanel1.Controls.Add(ShowPasswordButton);
		}

		void ShowPasswordButton_Click(object sender, EventArgs e)
		{
			string message ="";
			for (int i = 0; i < dataGridView1.Rows.Count; i++)
			{
				var item = dataGridView1.Rows[i];
				Crc32 crc32 = new Crc32();
				string id = Convert.ToString(item.Cells[1].Value);
				crc32.CrcAdd(id);
				string pw = Convert.ToString(crc32.CrcGet(), 16);
				message +=id+"\t\t"+pw+"\r\n";
			}
			ShowPassword showPassword = new ShowPassword(message);
			showPassword.ShowDialog();
		}

		void ViewPasswordButton_Click(object sender, EventArgs e)
		{
			Crc32 crc32 = new Crc32();
			string id = Convert.ToString(dataGridView1.CurrentRow.Cells[1].Value);
			crc32.CrcAdd(id);
			string pw = Convert.ToString(crc32.CrcGet(), 16);
			MessageBox.Show("用户“"+id+"”的登录密码为"+pw,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
		}

		void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
		{
			ScoreButton.Enabled = true;
		}
		
		void AcceptButton_Click(object sender, EventArgs e)
		{
			try
			{
				userInfoTableAdapter1.Update(databaseWRJDataSet1.UserInfo);
				MessageBox.Show("数据提交成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
			
		}
	}
}
